package com.clstu.qqclient.qqframe;

import com.clstu.qqclient.service.ClientConnectServerThread;
import com.clstu.qqclient.service.ManageClientConnectServerThread;
import com.clstu.qqclient.service.UserClientService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 登录后的主菜单类
 */
public class MainMenu extends JFrame implements ActionListener {
    //定义一些菜单按钮
    private JButton btOnlineList = new JButton("获取在线用户列表");
    private JButton btGroupSent = new JButton("群发消息");
    private JButton btPrivateSent = new JButton("私发消息");
    private JButton btSentFile = new JButton("私发文件");
    private JButton btExit = new JButton("退出登录");
    private String userID;
    private UserClientService userClientService = new UserClientService();

    public MainMenu(String userID){
        super("用户"+userID+"的主菜单,欢迎使用..!");
        this.userID = userID;
        setLayout(new GridLayout(5,1,20,10));//网格布局

        add(btOnlineList);//加入组件
        add(btGroupSent);
        add(btPrivateSent);
        add(btSentFile);
        add(btExit);

        //设置窗口属性
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocation(700,400);
        setResizable(false);
        setSize(500,300);

        //加入监听器
        btExit.addActionListener(this);
        btGroupSent.addActionListener(this);
        btPrivateSent.addActionListener(this);
        btSentFile.addActionListener(this);
        btOnlineList.addActionListener(this);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        ClientConnectServerThread cCST = ManageClientConnectServerThread.getClientConnectServerThread(userID);

        if(e.getSource().equals(btExit)){//如果按下的是退出登录键ok
             userClientService.logout(userID);
             ManageClientConnectServerThread.removeClientConnectServerThread(userID);
             this.dispose();
             new LoginFrame();

         }else if(e.getSource().equals(btGroupSent)){//如果是群发消息ok
             JTextFieldSentMessage jTextFieldSentMessage = new JTextFieldSentMessage(userID, true);

         }else if (e.getSource().equals(btPrivateSent)){//如果是私法消息ok
             JTextFieldSentMessage jTextFieldSentMessage = new JTextFieldSentMessage(userID);

         }else if(e.getSource().equals(btSentFile)){//如果是发送文件ok
             new SentFileFrame(userID);

         }else if(e.getSource().equals(btOnlineList)){//如果是获取在线列表ok
             this.dispose();
             userClientService.getOnline(userID);
         }
    }
}
